/*
 * Copyright (c) 2016- 2020 qolome.com Inc. All Rights Reserved.
 * ProjectName: qolome-knowledge
 * FileName: Proxy.java
 * Author: jiangfeng@qolome.com
 * Date: 2020/3/2 下午10:19
 * LastModified: 2020/03/02 22:19:02
 */

package com.qolome.knowledge.design.pattern.proxy.dynamicproxy.cglibproxy;

import lombok.extern.slf4j.Slf4j;
import org.springframework.cglib.proxy.Enhancer;
import org.springframework.cglib.proxy.MethodInterceptor;
import org.springframework.cglib.proxy.MethodProxy;

import java.lang.reflect.Method;

/**
 * @author Qolome
 * @version V1.0
 * @Description 代理
 * @date 2020年 03月02日 22:19:23
 */
@Slf4j
public class CGLibMatchMaker implements MethodInterceptor {

    public Object getInstance(Class<?> clazz) {
        Enhancer enhancer = new Enhancer();
        enhancer.setSuperclass(clazz);
        enhancer.setCallback(this);
        return enhancer.create();

    }


    @Override
    public Object intercept(Object o, Method method, Object[] objects, MethodProxy methodProxy) throws Throwable {
        before();
        Object result = methodProxy.invokeSuper(o, objects);
        after();
        return result;
    }

    private void after() {
        log.info("双方同意开始交往");
    }

    private void before() {
        log.info("屁大大婚姻中介所,欢迎你的光顾");
    }
}
